<template>
  <div>
    <!-- 添加职位 -->
    <div>
      <el-input
          size="small"
          class="addPosInput"
          placeholder="添加职位...."
          suffix-icon="el-icon-plus"
          v-model="pos.name"
          @keydown.enter.native="addPosition">
      </el-input>
      <el-button
          @click="addPosition"
          icon="el-icon-plus"
          size="small"
          type="primary"
          >添加</el-button>
    </div>
    <!-- 显示职位 -->
    <div class="posManaMain">
      <el-table
          size="small"
          stripe
          border
          :data="positions"
          @selection-change="handleSelectionChange"
          style="width: 70%">
        <!-- 多选框 -->
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职位"
            width="120">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间"
            width="200">
        </el-table-column>
        <!-- 操作 -->
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showEditView(scope.$index, scope.row)"
              >编辑</el-button>
            <el-button
                type="danger"
                size="mini"
                @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 批量删除 -->
    <el-button
        size="small"
        style="margin-top: 8px"
        type="danger"
        @click="deleteMany"
        :disabled="multipleSelection.length == 0">批量删除</el-button>
    <!-- 弹框 -->
    <el-dialog
        title="编辑职位"
        :visible.sync="dialogVisible"
        width="30%"
        >
      <div>
        <el-tag>职位名称</el-tag>
        <el-input size="small" v-model="updatePos.name" class="updatePosInput"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "PosMana",
  data(){
    return {
      pos: {
        name: '',
      },
      updatePos: {
        name: '',
      },
      inputPosition:'',
      positions:[],
      dialogVisible: false,
      multipleSelection: [],
    };
  },
  methods:{
    deleteMany(){
      this.$confirm(`此操作将永久删除[${this.multipleSelection.length}]条职位，是否继续？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        let ids = '?';
        this.multipleSelection.forEach(item=>{
          ids += 'ids=' + item.id + '&';
        })
        this.deleteRequest('/system/basic/pos/'+ ids).then(resp=>{
          if (resp){
            this.initPositions();
          }
        })
      })
    },
    // 处理选择
    handleSelectionChange(val){
      this.multipleSelection = val;
      console.log(val);

    },

    // 更新职位
    doUpdate(){
      this.putRequest('/system/basic/pos/', this.updatePos).then(resp=>{
        if (resp){
          // 修改成功
          this.initPositions();
          this.dialogVisible = false;
        }
      })
    },

    // 添加职位
    addPosition(){
      if (this.pos.name){
        // 添加
        this.postRequest('/system/basic/pos/', this.pos).then(resp=>{
          if (resp){
            // 添加成功
            // 刷新页面展示的列表信息
            this.initPositions();
            // 这里因为我们自己定义了 axios的拦截器，会显示相应信息，这里就不需要显示了
          }
          // 将输入框中输入的内容删除,无论添加是否成功，防止因网络慢，导致用户重复添加出现错误提示
          this.pos.name = '';
        })
      }else {
        // 提示输入格式错误信息
        this.$message.error('职位名称不能为空');
      }
    },
    initPositions(){
      this.getRequest('/system/basic/pos/').then(resp=>{
        if (resp){
          this.positions = resp;
        }
      })
    },
    // 编辑按钮点击逻辑实现
    showEditView(index,data){
      // this.updatePos = data;
      Object.assign(this.updatePos, data);
      this.updatePos.createDate = '';
      this.dialogVisible = true;
    },
    // 删除按钮点击逻辑实现
    handleDelete(index,data){
      // 提示用户删除的重要性
      this.$confirm(`此操作将永久删除[${data.name}],是否继续？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        // 点击了确定按钮
        this.deleteRequest(`/system/basic/pos/${data.id}`).then(resp=>{
          if (resp){
            // 删除成功
            // 再次更新展示的列表信心
            this.initPositions();
          }
        })
      }).catch(()=>{
        // 点击了取消按钮
      })
    }
  },
  // 页面DOM 加载之前，get请求获得数据
  mounted() {
    this.initPositions();
  }
}
</script>

<style scoped>
  .addPosInput{
    width: 300px;
    margin-right: 8px;
  }
  .posManaMain{
    margin-top: 10px;
  }
  .updatePosInput{
    width: 200px;
    margin-left: 8px;
  }
</style>